package com.jse;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.jse.json.Json;

//import com.jse.json.Json;
//import com.jse.json.JsonObject;

public class Trees {

	public static void main(String[] args) throws IOException {
		//模拟从数据库查询出来
//        List<Map> menus = Arrays.asList(
//        		Lang.ofMap("id",1,"name","根节点1","pid",0),
//                Lang.ofMap("id",2,"name","根节点1.2","pid",1),
//                Lang.ofMap("id",3,"name","根节点1.3","pid",1),
//                Lang.ofMap("id",4,"name","根节点4","pid",0),
//                Lang.ofMap("id",5,"name","根节点4.5","pid",4),
//                Lang.ofMap("id",6,"name","根节点4.6","pid",4),
//                Lang.ofMap("id",7,"name","根节点4.7","pid",4),
//                Lang.ofMap("id",8,"name","根节点4.5.1","pid",5),
//                Lang.ofMap("id",9,"name","根节点4.5.2","pid",5),
//                Lang.ofMap("id",10,"name","根节点4.6.2","pid",6),
//                Lang.ofMap("id",11,"name","根节点4.6.3","pid",6),
//                Lang.ofMap("id",12,"name","根节点12","pid",0)
//        );
		List menus=Json.jsonArray("""
				[{"id":1,"pid":0,"name":"内容","type":"目录","icon":null,"link":"/","target":null},{"id":2,"pid":0,"name":"报名","type":"目录","icon":null,"link":null,"target":null},{"id":3,"pid":1,"name":"分类管理","type":"菜单","icon":null,"link":null,"target":null},{"id":4,"pid":1,"name":"栏目管理","type":"菜单","icon":null,"link":null,"target":null},{"id":5,"pid":1,"name":"科学普及","type":"菜单","icon":null,"link":null,"target":null},{"id":6,"pid":1,"name":"继续教育","type":"菜单","icon":null,"link":null,"target":null},{"id":7,"pid":2,"name":"协会刊物","type":"菜单","icon":null,"link":null,"target":null},{"id":8,"pid":2,"name":"分支机构","type":"菜单","icon":null,"link":null,"target":null},{"id":9,"pid":2,"name":"会员专区","type":"菜单","icon":null,"link":null,"target":null},{"id":10,"pid":2,"name":"下载专区","type":"菜单","icon":null,"link":null,"target":null}]
				"""); //new ArrayList<Map>();
		
        //获取父节点
        List<Map> collect = menus(menus);
//        System.out.println(menus);
        System.out.println("-------转json输出结果-------");
        System.out.println(collect);
        System.out.println(Json.toJson(ftree("d:/uniapp")));
	}
	public static List<Map> menus(List<Map> list){
		return menus(list,"pid");
	}
	public static List<Map> menus(List<Map> list,String name){
		return menus(list,name,"children");
	}
	public static List<Map> menus(List<Map> list,String name,String children){
		return menus(list,name,"children",0,"pid","id");
	}
	public static List<Map> menus(List<Map> list,Map map){
		String name=map.getOrDefault("name","pid").toString();
		String children=map.getOrDefault("children","children").toString();
		String pidName=map.getOrDefault("pidname","pid").toString();
		String idName=map.getOrDefault("idname","id").toString();
		Object pid=map.getOrDefault(pidName,0).toString();
		return menus(list,name,children,pid,pidName,idName);
	}
	
	public static List<Map> menus(List<Map> list,String name,String children,Object pid,String pidName,String idName){
		return list.stream().filter(m -> m.get(name).equals(pid)).map(
                (m) -> {m.put(children,getChildrens(m, list,children,pidName,idName));return m;}
        ).collect(Collectors.toList());
	}
	/**
     * 递归查询子节点
     * @param root  根节点
     * @param all   所有节点
     * @return 根节点信息
     */
    private static List<Map> getChildrens(Map root, List<Map> all,String childrenName,String pid,String id) {
        List<Map> children = all.stream().filter(m -> {
            return Objects.equals(m.get(pid), root.get(id));
        }).map((m) -> {m.put(childrenName,getChildrens(m, all,childrenName,pid,id));return m;}
        ).collect(Collectors.toList());
        return children;
    }
    
    public static List<Map> ftree(String path) throws IOException{
    	List<Map> menus=new ArrayList<Map>();
    	var root=Path.of(path);
		Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
			@Override
			public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
				if(dir.toString().equals(root.toString())) {
					menus.add(Lang.ofMap("id",dir.toString(),"pid",0));
				}else {
					menus.add(Lang.ofMap("id",dir.toString(),"pid",dir.getParent().toString()));
				}
				return super.preVisitDirectory(dir, attrs);
			}
			@Override
			public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
				menus.add(Lang.ofMap("id",file.toString(),"pid",file.getParent().toString()));
				return super.visitFile(file, attrs);
			}
		});
		return menus(menus);
    }
}
